/*
 * Copyright (C) 2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jiaomatech.tencent.cos;

import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.region.Region;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * Description: 私有云cos的配置文件写入 .<br>
 *
 * @author jm
 */
@ConfigurationProperties(prefix = CosConstants.PREFIX)
public class CosProperties {

    public static final Map<String, String> endpointMap = new HashMap<>();
    /**
     * 日志.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(CosProperties.class);

    static {
        endpointMap.put("ap-beijing-1", "http://<bucketname-APPID>.cos.ap-beijing-1.myqcloud.com");
        endpointMap.put("ap-beijing", "http://<bucketname-APPID>.cos.ap-beijing.myqcloud.com");
        endpointMap.put("ap-shanghai", "http://<bucketname-APPID>.cos.ap-shanghai.myqcloud.com");
        endpointMap.put("ap-guangzhou", "http://<bucketname-APPID>.cos.ap-guangzhou.myqcloud.com");
        endpointMap.put("ap-chengdu", "http://<bucketname-APPID>.cos.ap-chengdu.myqcloud.com");
        endpointMap.put("ap-chongqing", "http://<bucketname-APPID>.cos.ap-chongqing.myqcloud.com");
        endpointMap.put("ap-hongkong", "http://<bucketname-APPID>.cos.ap-hongkong.myqcloud.com");
    }

    private ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing"));

    private String enabled = "true";

    private String accessKeyId;

    private String secretAccessKey;

    private String region = "ap-beijing";

    private String bucketName = "";

    private String endpoint = "http://<bucketname-APPID>.cos.ap-beijing.myqcloud.com";

    private String securityToken;

    public String getEnabled() {
        return enabled;
    }

    public void setEnabled(String enabled) {
        this.enabled = enabled;
    }

    public ClientConfig getClientConfig() {
        return clientConfig;
    }

    public void setClientConfig(ClientConfig clientConfig) {
        this.clientConfig = clientConfig;
    }

    public String getAccessKeyId() {
        return accessKeyId;
    }

    public void setAccessKeyId(String accessKeyId) {
        this.accessKeyId = accessKeyId;
    }

    public String getSecretAccessKey() {
        return secretAccessKey;
    }

    public void setSecretAccessKey(String secretAccessKey) {
        this.secretAccessKey = secretAccessKey;
    }

    public String getEndpoint() {
        return endpoint;
    }

    public void setEndpoint(String endpoint) {
        this.endpoint = endpoint;
    }

    public String getSecurityToken() {
        return securityToken;
    }

    public void setSecurityToken(String securityToken) {
        this.securityToken = securityToken;
    }

    public String getRegion() {
        return region;
    }

    public void setRegion(String region) {
        if (!endpointMap.containsKey(region)) {
            LOGGER.error("[region:{}] - [请选择以下的region: {}]", region,
                Arrays.toString(endpointMap.keySet().toArray()));
            throw new IllegalArgumentException("error region config");
        }
        this.region = region;

        this.setEndpoint(endpointMap.get(region));
        if (Objects.isNull(this.clientConfig)) {
            this.clientConfig = new ClientConfig(new Region(region));
        } else {
            this.clientConfig.setRegion(new Region(region));
        }
    }

    public String getBucketName() {
        return bucketName;
    }

    public void setBucketName(String bucketName) {
        this.bucketName = bucketName;
    }
}
